package com.byzk.p2p.front.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.user.dao.InviteRefDao;
import com.byzk.p2p.admin.user.dto.User;
import com.byzk.p2p.admin.user.dto.InviteRef;
import com.byzk.p2p.admin.user.vo.InviteRefVo;
import com.byzk.p2p.front.dao.FrontUserDao;
import com.byzk.p2p.front.vo.ProtocolVo;

/**
 * 前台用户service
 * @author suker.zhou
 *
 */
@Service
public class FrontUserService {

	@Autowired
	private FrontUserDao frontUserDao;
	@Autowired
	private InviteRefDao inviteRefDao;
	
	/**
	 * 用户注册
	 * @param user
	 * @return
	 */
	public Integer saveOrUpdate(User user){
		if(user.getId()==null){
			return frontUserDao.insert(user);
		}else{
			return frontUserDao.update(user);
		}
	}
	
	public List<User> getByUser(User user){
		return frontUserDao.getByUser(user);
	}
	
	public User selectByNameOrPhone(String username){
		return frontUserDao.selectByNameOrPhone(username);
	}
	
	public User selectByInviteCode(String inviteCode){
		return frontUserDao.selectByInviteCode(inviteCode);
	}
	
	/**
	 * 根据邀请码获取用户
	 * @param inviteCode 邀请码
	 * @return
	 */
	public User getUserByInviteCode(String inviteCode){
		return frontUserDao.getUserByInviteCode(inviteCode);
	}
	
	
	/**
	 * 保存用户邀请关系
	 * @param inviteRef
	 * @return
	 */
	public Integer saveOrUpdateInviteRef(InviteRef inviteRef){
		if (inviteRef.getId() == null) {
			return inviteRefDao.insert(inviteRef);
		}
		return null;
	}
	
	/**
	 * 根据被邀请人用户ID，获取邀请关系。
	 * @return
	 */
	public InviteRef getInviteRefByUserId(Integer userId){
		return inviteRefDao.getInviteRefByUserId(userId);
	}
	
	/**
	 * 获取所有的邀请用户
	 * @param vo
	 * @return
	 */
	public PageInfo<InviteRefVo> listInviteRef(InviteRefVo vo){
		int count=inviteRefDao.inviteCount(vo);
		List<InviteRefVo> list =inviteRefDao.listInvite(vo);
		PageInfo<InviteRefVo> page=new PageInfo<InviteRefVo>(count,vo.getIndex() ,vo.getLimit());
		page.setPageList(list);
		return page;
	}
	
	public Integer deleteInviteRef(Integer id){
		return inviteRefDao.deleteInviteRef(id);
	}
	
	/**
	 * 根据邀请用户id获取受邀请人数
	 * @param inviteUserId 邀请用户id
	 * @return 
	 */
	public Integer getInviteCount(Integer inviteUserId){
		InviteRefVo vo = new InviteRefVo();
		vo.setPid(inviteUserId);
		int count=inviteRefDao.inviteCount(vo);
		return count;
	}
	
	public Integer inviteCountToPid(Integer pid){
		return inviteRefDao.inviteCountToPid(pid);
	}
	
	public User getUserById(Integer id){
		return frontUserDao.selectByUserId(id);
	}
	
	public ProtocolVo getByUserIdAndBorrowId(Integer userId,Integer borrowId){
		return frontUserDao.selectByUserIdAndBorrowId(userId, borrowId);
	}

	/**
	* <p>Description:更新老用户登录加密密码 </p>
	* <p>Company:xbd </p>
	* @author:lilei
	* @date:2016年9月22日上午9:18:42
	 */
	public Integer updateOldUserPwd(String passWord_,String  isFrist,Integer uid){
		User user = new User();
		user.setPassword(passWord_);
		user.setIsfristLogin(isFrist);
		user.setId(uid);
		return frontUserDao.update(user);
	}
} 
